package com.slg.android.manyanime;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.slg.android.manyanime.adapter.MyCommentAdapter;
import com.slg.android.manyanime.bean.MyComment;
import com.slg.android.manyanime.bean.User;
import com.slg.android.manyanime.utils.DataStore;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;


public class MyCommentActivity extends Activity implements View.OnClickListener {

    private List<MyComment> myComments;
    private MyCommentAdapter adapter;
    private ListView listView;
    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_comment);

        View view = findViewById(R.id.my_comment_list_return_img);
        view.setOnClickListener(this);
        user = DataStore.getDataStore().getUser();
        listView = (ListView) findViewById(R.id.my_comment_content_list);

        myComments = new LinkedList<MyComment>();
        adapter = new MyCommentAdapter(this, myComments);

        if(listView != null) {
            listView.setAdapter(adapter);
            adapter.setOperationListener(this);
              HttpUtils httpUtils = new HttpUtils();
                //todo ！！！此处为固定接口，地址需要更改 已经修改
                httpUtils.send(HttpRequest.HttpMethod.GET,
                        "http://manhua.api.61.com/?&pf=1&android_version=16&app_version=512" +
                                "&app_channel=百度&m=Index&a=commentByUid&uid="+user.getId()+"&last_id=" +
                                "&mid=&module=&order=&login_uid="+user.getId(),
                        new RequestCallBack<String>() {
                            @Override
                            public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                                String result = stringResponseInfo.result;
                                LinkedList<MyComment> myCommentList = new LinkedList<MyComment>();
                                try {
                                    JSONObject jsonObject = new JSONObject(result);
                                    JSONArray jsonArray = jsonObject.getJSONArray("list");
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        JSONObject commentJSONObject = jsonArray.getJSONObject(i);
                                        MyComment myComment = new MyComment();
                                        myComment.parseJSON(commentJSONObject);
//                                        Log.d("MyCommentActivity------>", myComment.toString() + "");
                                        myCommentList.add(myComment);
                                    }
                                    myComments.addAll(myCommentList);
//                                    Log.d("MyCommentActivity------>", myComments.size() + "");
                                    adapter.notifyDataSetChanged();

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onFailure(HttpException e, String s) {
                                Toast.makeText(MyCommentActivity.this, "网络问题", Toast.LENGTH_LONG).show();
                            }
                        });
            }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.my_comment_list_return_img :
                finish();
                break;
            case R.id.item_my_comment_img:

                break;
            case R.id.item_my_comment_zan_hand:
                Object tag = v.getTag();
                if(tag != null){
                    if(tag instanceof Integer){
                        int pos = (Integer)tag;
                        MyComment item = myComments.get(pos);
                        int good_num = Integer.valueOf(item.getGood_num());
                        if(v.isSelected()){
                            v.setBackgroundResource(R.drawable.content_pro_hover);
                            good_num++;
                        }else{
                            v.setBackgroundResource(R.drawable.content_pro_default);
                            good_num--;
                        }


                        item.setGood_num(Integer.toString(good_num));

                        adapter = new MyCommentAdapter(this, myComments);
                        listView.setAdapter(adapter);

                       // myComments.clear();

                       // adapter.notifyDataSetChanged();

                        adapter.notifyDataSetChanged();

                    }
                }
                break;
            case R.id.item_my_comment_picture_comment:

                break;


        }

    }
}
